<?php

/* TODO: Add code here */
/**
 * Lớp đối tượng của FAQ phụ trách tương tác với cơ sở dữ liệu và những phương thức khác liên quan.
 * 
 **/
class user_user
{
	#region user_common
	const ACT_ADD							= 10;
	const ACT_UPDATE						= 11;
	const ACT_DELETE						= 12;
	const ACT_CHANGE_PAGE					= 13;
	const ACT_SHOW_EDIT = 14;
	const ACT_LOGIN = 15;
	const ACT_LOGOUT = 16;
	const NUM_PER_PAGE = 10;
	const TBL_T_USER			= 't_user';
	const TBL_T_ADMIN_TYPE			= 'admin_type';
	const SQL_INSERT_USER			= 'INSERT INTO `{0}`
		(`user_id`,`name`,`password`,`admin_type`)
		VALUES (\'{1}\',\'{2}\',\'{3}\',\'{4}\');';
		
	const SQL_UPDATE_USER			= 'UPDATE `{0}`
		SET 
		`name` = \'{1}\',
		`detail` = \'{2}\'
		WHERE `section_id` = \'{3}\';';
	
	const SQL_CREATE_TABLE_T_USER		= 'CREATE TABLE `{0}` (
		`section_id` bigint(20) NOT NULL COMMENT \'Mã ngành hàng tăng tự động\',
		`name` char(255) COLLATE utf8_unicode_ci DEFAULT NULL COMMENT \'Tên ngành hàng\',
		`detail` text COLLATE utf8_unicode_ci COMMENT \'Thông tin thêm về ngành hàng\',
		`delete_flag` tinyint(4) DEFAULT NULL COMMENT \'0: avaiable,1 : delete\',
		PRIMARY KEY (`section_id`)
		) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;';
	#end region
	
	
	#region Variables
	var $_objConnection;
	#end region
	
	
	#region Contructors
	/**
	*  Phương thức khởi tạo đối tượng faq đồng thời tạo connection đến db
	*
	* @param object $objConnection Đối tượng kết nối đến db
			
	* @return void 
	*
	*/
	public function user_USER($objConnection)
	{
		$this->_objConnection = $objConnection;
		
	}
	#end region
	
	
	#region Private Functions
	
	#end region
	
	
	#region Public Functions
	public function insert($intID,$pass, $name,$type) 
	{
		$strSQL = user_common::prepareQuery(self::SQL_INSERT_USER,
				array(self::TBL_T_USER,$intID,user_common::escape_mysql_string($name),user_common::escape_mysql_string($pass),user_common::escape_mysql_string($type) ));
		$strTableName = self::TBL_T_USER;
		if (!user_common::ExecutequeryWithCheckExistedTable($strSQL,self::SQL_CREATE_TABLE_T_USER,$this->_objConnection,$strTableName))
		{
			//echo $strSQL;
			user_common::writeLog('Error add user:'.$strSQL,1);
			return false;
		}	
		return $intID;
		
	}
	
	/**
	 * Thêm mới một nhà sản xuất
	 *
	 * @param int $m_USER_id Mã nhà sản xuất
	 * @param int $m_cat_id Mã nhóm
	 * @param string $name Tên sản phẩm
	 * @param string $m_image Tên file hình ảnh
	 * @return bool True->Thành công, False->thất bại
	 *
	 */
	public function update($intID,$name,$detail) 
	{
		$strSQL = user_common::prepareQuery(self::SQL_UPDATE_USER,
				array(self::TBL_T_USER,user_common::escape_mysql_string($name),user_common::escape_mysql_string($detail),$intID ));
		
		if (!user_common::ExecutequeryWithCheckExistedTable($strSQL,self::SQL_CREATE_TABLE_T_USER,$this->_objConnection,$strTableName))
		{
			//echo $strSQL;
			user_common::writeLog('Error add section:'.$strSQL,1);
			return false;
		}	
		return $intID;
		
	}
	
	
	public function getUserByID($m_id,$selectField='*') 
	{
		$strSQL .= user_common::prepareQuery(user_common::SQL_SELECT_FREE, array($selectField, self::TBL_T_USER ,' where `user_id`=\''.$m_id.'\' '));
		//echo '<br>SQL:'.$strSQL;
		$arrResult =$this->_objConnection->selectCommand($strSQL);
		//print_r($arrResult);
		//echo '<br>';
		user_common::writeLog('getUserByID:'.$strSQL,1,$_mainFrame->pPage);
		if(!$arrResult[0])
		{
			user_common::writeLog('getUserByID:'.$strSQL,1,$_mainFrame->pPage);
			return null;
		}	
		//print_r($arrResult);
		return $arrResult[0];
	}
	public function getListUser($intPage, $where)
	{
		$strSQL .= user_common::prepareQuery(user_common::SQL_SELECT_FREE,array('*',
					self::TBL_T_USER,' WHERE delete_flag IS NULL or delete_flag=\'0\' ORDER BY `user_id` ASC  limit '.(($intPage-1)* self::NUM_PER_PAGE).','.self::NUM_PER_PAGE));
		
		$arrUSer = $this->_objConnection->selectCommand($strSQL);
		
		$arrAdminType = $this->getAllAdminType();
		//print_r($arrSection);
		if ($arrAdminType!=null)
		{
			foreach ($arrAdminType as $item)
			{
				$arrSectionByKey[$item['admin_id']] = $item;
			}
		}
		//print_r($arrCat);
		//print_r($arrSectionByKey);
		//chuyển kết quả vào danh sách ID đã theo thứ tự
		$icount = count($arrUSer);
		for($i=0;$i<$icount;$i++)
		{
			//echo $arrCatalogue[$i]['section_id'];
			$arrUSer[$i]['admin_name'] = $arrSectionByKey[$arrUSer[$i]['admin_type']]['name'];
			//echo $arrSectionByKey[$arrCatalogue[$i]['section_id']]['name'];
		}
		//print_r($arrUSer);
		$strHTML = ' <table class="tbl-list">
				<thead>
				<td>Mã nhân viên</td>
				<td>Họ tên</td>
				<td>Loại admin</td>
				<td>Display</td>
				<td class="lastCell"></td>
				</thead>
				<tbody>';
		
		for($i=0;$i<$icount;$i++)
		{
			$strHTML.='<tr class="'.($i%2==0?'even':'odd').'">
					<td>'.$arrUSer[$i]['user_id'].'</td>
					<td>'.$arrUSer[$i]['name'].'</td>
					<td>'.$arrUSer[$i]['admin_name'].'</td>
					<td><input type="checkbox" onclick="objArticleMn.showHideArticle(this)" '.($arrUSer['delete_flag']?'':'checked=checked').' /></td>
					<td class="lastCell"><b><a href="javascript:_objUser.showEdit(\''.$arrUSer[$i]['user_id'].'\',this);">Edit</a></b></td>
					</tr>';
		}
		$strHTML.='</tbody></table>';
		
		$strHTML .= "<div>".user_common::getPagingHTMLByNum($intPage,self::NUM_PER_PAGE,user_common::getTotalRecord(self::TBL_T_USER,$this->_objConnection),
				"_objUser.changePage")."</div>";
		return $strHTML;
	}
	
	
	/**
	 * Hàm lấy tất cả record của c_doc_xxxx dựa vào danh sách doc_id truyền vào dùng cho việc search.
	 *	
	 * @param string $selectField Cho biết các field cần lấy.
	 * @return array Danh sách record 
	 * @author ThanhViet added [20101005]
	 */
	public function getAllSection($selectField='*') 
	{
		/*$arrDocID = Array();
		foreach($listID as $item)
		{
			$arrDocID[$item['id']] = $item['id'];
		}*/
		//echo '<br>count:'.count($listID);
		
		$strSQL .= user_common::prepareQuery(user_common::SQL_SELECT_FREE, array($selectField, self::TBL_T_USER ,' order by name'));
		//echo '<br>SQL:'.$strSQL;
		$arrResult =$this->_objConnection->selectCommand($strSQL);
		//print_r($arrResult);
		//echo '<br>';
		if(!$arrResult)
		{
			user_common::writeLog('getAllSection:'.$strSQL,1,$_mainFrame->pPage);
			return null;
		}	
		//print_r($arrResult);
		return $arrResult;
	}
	
	/**
	 * This is method outputHTMLSection select option
	 *
	 * @param mixed $arrFaq This is a description
	 * @param mixed $strKeyword This is a description
	 * @return mixed This is the return value description
	 *
	 */
	public function outputHTMLSection($arrUSer)
	{
		$strHTML = '<select id="txtSect" name="txtSect" value="" style="width: 50%;" onchange="_objAdminProduct.loadCatalogue()">';
		$strHTML .= '<option value="0">--Chọn ngành hàng--</option>';
		
		foreach($arrUSer as $item)
		{
			$strHTML .= '<option value="'.$item['section_id'].'">'.$item['name'].'</option>';
		}
		$strHTML .= '</select>';
		return $strHTML;
	}
	
	public function getAllAdminType($selectField='*') 
	{
		
		$strSQL .= user_common::prepareQuery(user_common::SQL_SELECT_FREE, array($selectField, self::TBL_T_ADMIN_TYPE ,' order by name'));
		//echo '<br>SQL:'.$strSQL;
		$arrResult =$this->_objConnection->selectCommand($strSQL);
		//print_r($arrResult);
		//echo '<br>';
		if(!$arrResult)
		{
			user_common::writeLog('getAllAdminType:'.$strSQL,1,$_mainFrame->pPage);
			return null;
		}	
		//print_r($arrResult);
		return $arrResult;
	}
	public function outputHTMLAdminType($arrAdmin)
	{
		$strHTML = '<select id="txtAdminType" name="txtAdminType" value="" style="width: 50%;" >';
		$strHTML .= '<option value="0">--Chọn loại admin--</option>';
		
		foreach($arrAdmin as $item)
		{
			$strHTML .= '<option value="'.$item['admin_id'].'">'.$item['name'].'</option>';
		}
		$strHTML .= '</select>';
		return $strHTML;
	}
	
	public function getPassword($user_id,$pass) 
	{
		//echo $user_id.$pass.'->';
		return md5($user_id.$pass);
	}
	
	public function login($user_id,$pass) 
	{
		//echo $pass;
		$enPass = $this->getPassword($user_id,$pass);
		//echo $enPass;
		$objUser = $this->getUserByID($user_id);
		//print_r($objUser);
		if ($objUser && $objUser['password'] == $enPass) 
		{
			//print_r($objUser);
			return $objUser;
		}
		else
		{
			return false;	
		}
	}
	
	
	#end region
}

?>